package com.aiju.cash.ui.main;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;

import com.aiju.base.activity.BaseTitleActivity;
import com.aiju.cash.R;
import com.aiju.utils.CRequest;
import com.aiju.utils.T;
import com.uuzuche.lib_zxing.activity.CaptureFragment;
import com.uuzuche.lib_zxing.activity.CodeUtils;

import java.util.Map;

/**
 * @author: 流冥
 * @Description: 首页点击右上角图标进入扫一扫，扫描erp 二维码
 */
public class RichScanActivity extends BaseTitleActivity {
    CaptureFragment captureFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rich_scan);
        initView();
    }

    void initView() {
        mAiJuTitleBarView.setCenterTitle("扫一扫");
        addFragment();

    }

    void addFragment() {
        captureFragment = getCaptureFragment();
        getSupportFragmentManager().beginTransaction().add(R.id.content, captureFragment).commit();

    }

    /**
     * 二维码
     *
     * @return
     */
    private CaptureFragment getCaptureFragment() {
        CaptureFragment captureFragment = new CaptureFragment();
        CodeUtils.setFragmentArgs(captureFragment, R.layout.my_camera_rich_scan);
        captureFragment.setAnalyzeCallback(analyzeCallback);
        return captureFragment;
    }


    /**
     * 二维码解析回调函数
     */
    private final CodeUtils.AnalyzeCallback analyzeCallback = new CodeUtils.AnalyzeCallback() {

        @Override
        public void onAnalyzeSuccess(Bitmap mBitmap, String result) {
            Map<String, String> stringStringMap = CRequest.URLRequest(result);
            String pStoreId = stringStringMap.get("pstoreid");
            if (result.indexOf("rid") > 0 && result.indexOf("storeIdList") > 0 && result.indexOf("pStoreId") > 0 && mUser.getParentStoreId().equals(pStoreId)) {
                Intent intent = new Intent(RichScanActivity.this, BindStoreActivity.class);
                intent.putExtra("url", result);
                startActivity(intent);
                finish();
            } else {
                T.ToastShort("请扫描ERP系统的二维码");
                finish();
            }

        }

        @Override
        public void onAnalyzeFailed() {
            T.ToastShort("扫描失败，请重试");
        }
    };


}
